package com.chenxy.gateway.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.http.codec.multipart.MultipartHttpMessageReader;
import org.springframework.http.codec.multipart.SynchronossPartHttpMessageReader;
import org.springframework.web.reactive.config.EnableWebFlux;
import org.springframework.web.reactive.config.WebFluxConfigurer;

/**
 * webflux上传文件设置
 * @author  chenxy
 */
@Configuration
@EnableWebFlux
public class UploadFileConfig implements WebFluxConfigurer {

    /**
     * 最大文件数量
     */
    private String maxparts = "1";
    /**
     * 文件大小
     */
    private String maxFileSize = "5242880";

    public void configureHttpMessageCodecs(ServerCodecConfigurer configurer)
    {
        SynchronossPartHttpMessageReader partReader = new SynchronossPartHttpMessageReader();
        partReader.setMaxParts(Integer.parseInt(this.maxparts));

        partReader.setMaxDiskUsagePerPart(Integer.parseInt(this.maxFileSize));
        partReader.setEnableLoggingRequestDetails(true);


        MultipartHttpMessageReader multipartReader = new MultipartHttpMessageReader(partReader);
        multipartReader.setEnableLoggingRequestDetails(true);
        configurer.defaultCodecs().multipartReader(multipartReader);
    }
}
